{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Serving ML Predictions in batch and real-time\n",
    "\n",
    "**Learning Objectives**\n",
    "1. Copy trained model into your bucket\n",
    "2. Deploy AI Platform trained model\n",
    "\n",
    "## Introduction\n",
    "\n",
    "In this notebook, we will create a prediction service that calls your trained model deployed in Cloud to serve predictions. \n",
    "\n",
    "Each learning objective will correspond to a __#TODO__ in the [student lab notebook](../labs/serving_ml_prediction.ipynb) -- try to complete that notebook first before reviewing this solution notebook. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Copy trained model\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set necessary variables\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "PROJECT = \"cloud-training-demos\"  # Replace with your PROJECT\n",
    "BUCKET = PROJECT \n",
    "REGION = \"us-central1\"            # Choose an available region for Cloud MLE\n",
    "TFVERSION = \"2.6\"                # TF version for CMLE to use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"BUCKET\"] = BUCKET\n",
    "os.environ[\"PROJECT\"] = PROJECT\n",
    "os.environ[\"REGION\"] = REGION\n",
    "os.environ[\"TFVERSION\"] = TFVERSION"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a bucket and copy trained model in it\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "if ! gcloud storage ls --recursive gs://${BUCKET} | grep -q gs://${BUCKET}/babyweight/trained_model/; then\n",
    "    gcloud storage buckets create --location ${REGION} gs://${BUCKET}\n",
    "    # copy canonical model if you didn't do previous notebook\n",
    "    # TODO\n",
    "    gcloud storage cp --recursive gs://cloud-training-demos/babyweight/trained_model gs://${BUCKET}/babyweight\n",
    "fi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Deploy trained model\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We'll now deploy our model. This will take a few minutes. Once the cell below completes, you should be able to see your newly deployed model in the 'Models' portion of the AI Platform section of the GCP console."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "# Set necessary variables: \n",
    "MODEL_NAME=\"babyweight\"\n",
    "MODEL_VERSION=\"ml_on_gcp\"\n",
    "MODEL_LOCATION=$(gcloud storage ls gs://${BUCKET}/babyweight/export/exporter/ | tail -1)\n",
    "\n",
    "# Set the region to global by executing the following command: \n",
    "gcloud config set ai_platform/region global\n",
    "\n",
    "echo \"Deploying the model '$MODEL_NAME', version '$MODEL_VERSION' from $MODEL_LOCATION\"\n",
    "echo \"... this will take a few minutes\"\n",
    "\n",
    "# Deploy trained model: \n",
    "gcloud ai-platform models create ${MODEL_NAME} --regions $REGION\n",
    "# Create a new AI Platform version.\n",
    "# TODO\n",
    "gcloud ai-platform versions create ${MODEL_VERSION} \\\n",
    "  --model ${MODEL_NAME} \\\n",
    "  --origin ${MODEL_LOCATION} \\\n",
    "  --runtime-version $TFVERSION"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright 2021 Google Inc. Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
